package cn.com.scitc.user;

import cn.com.scitc.dao.ManagerDao;
import cn.com.scitc.model.Manager;
import cn.com.scitc.server.MyMd5;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "UserCreatServlet",urlPatterns = "/manager/user/creat")
public class UserCreatServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Manager manager = new Manager();

        try{
            if(!validation(request)){
                response.sendRedirect("new");
                return;
            }
            String loginId = request.getParameter("loginId");
            String realName = request.getParameter("realName");
            String checkPwd = request.getParameter("checkPwd");
            Date lastLoginDt = null;

            if (!request.getParameter("lastLoginDt").equals("")){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                lastLoginDt = sdf.parse(request.getParameter("lastLoginDt").replace("T"," "));
            }

            checkPwd = MyMd5.signPwd(checkPwd);

            manager.setLoginid(loginId);
            manager.setRealname(realName);
            manager.setPwd(checkPwd);
            manager.setLogindate(lastLoginDt);

            ManagerDao managerDao = new ManagerDao();
            managerDao.insert(manager);
            System.out.println("insert success!!");
            request.getSession().setAttribute("msg","新增用户成功");
            response.sendRedirect("list");

        }catch (Exception e){
            System.out.println("insert fail!!");
            request.getSession().setAttribute("msg","新增用户失败");
            request.getSession().setAttribute("manager",manager);
            response.sendRedirect("new");
            e.printStackTrace();
        }
    }
    private boolean validation(HttpServletRequest request){
        String loginId = request.getParameter("loginId");
        if(loginId == null || loginId.length() < 8 || loginId.length() > 8){
            System.out.println("账号不通过！！");
            return false;
        }

        String realName = request.getParameter("realName");
        if(realName == null || realName.length() < 2 || realName.length() > 5){
            System.out.println("姓名不通过！！" + realName);
            return false;
        }

        String pwd = request.getParameter("pwd");
        if(pwd == null || pwd.length() < 6 || pwd.length() > 20){
            System.out.println("密码不通过！！");
            return false;
        }

        String checkPwd = request.getParameter("checkPwd");
        if(!checkPwd.equals(pwd)){
            System.out.println("二次密码不通过！！");
            return false;
        }

        return true;
    }
}
